package com.pybox.api;

import com.pybox.aop.Mylog;
import com.pybox.dto.GroupObjectsDto;
import com.pybox.dto.GroupUsersDto;
import com.pybox.dto.Result;
import com.pybox.dto.TGroupsDto;
import com.pybox.service.TGroupsService;
import com.pybox.service.TWeekTimeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
* 描述：TGroups Api
* @author wuyingkai
* @date 2018-09-15
*/

@Slf4j
@RequestMapping("/TGroups")
@Api(tags = "TGroups管理接口", description = "TGroups管理接口")
@RestController
public class TGroupsApi {

    @Autowired
    private TGroupsService service;
    @Autowired
    private TWeekTimeService weekTimeService;


    @ApiOperation(value = "创建TGroups", notes = "创建TGroups")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @Mylog(value = "创建了分组信息")
    public Result<Boolean> save(@RequestBody TGroupsDto dto) {
        service.create(dto);
        return Result.ok(true);
    }

    @ApiOperation(value = "删除TGroups", notes = "删除TGroups")
    @Mylog(value = "删除了分组信息")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    @ApiImplicitParam(name = "id", value = "TGroups id", dataType = "Integer", paramType = "path")
    public Result<Boolean> delete(@PathVariable("id") Integer id) {
        service.delete(id);
        return Result.ok(true);
    }

    @ApiOperation(value = "更新TGroups", notes = "更新TGroups")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.PUT)
    @Mylog(value = "更新了分组信息")
    @ApiImplicitParam(name = "id", value = "TGroups id", dataType = "Integer", paramType = "path")
    public Result<Boolean> update(@PathVariable("id") Integer id, @RequestBody TGroupsDto dto) {
        service.update(id, dto);
        return Result.ok(true);
    }

    @ApiOperation(value = "查询TGroups", notes = "查询TGroups")
    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    @ApiImplicitParam(name = "id", value = "TGroups id", dataType = "Integer", paramType = "path")
    public Result<TGroupsDto> detail(@PathVariable("id") Integer id) {

        return Result.ok(service.detail(id));
    }

    @ApiOperation(value = "分页查询TGroups", notes = "分页查询TGroups")
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    @ApiImplicitParams({
           @ApiImplicitParam(name = "id", value = "id", dataType = "Integer", paramType = "query"),
           @ApiImplicitParam(name = "customerId", value = "customerId", dataType = "string", paramType = "query"),
           @ApiImplicitParam(name = "name", value = "name", dataType = "string", paramType = "query"),
           @ApiImplicitParam(name = "comments", value = "comments", dataType = "string", paramType = "query"),
           @ApiImplicitParam(name = "timeChannels", value = "timeChannels", dataType = "string", paramType = "query"),
           @ApiImplicitParam(name = "deleted", value = "deleted", dataType = "string", paramType = "query"),
           @ApiImplicitParam(name = "ctime", value = "ctime", dataType = "string", paramType = "query"),
           @ApiImplicitParam(name = "utime", value = "utime", dataType = "string", paramType = "query"),
           @ApiImplicitParam(name = "createby", value = "createby", dataType = "string", paramType = "query"),
           @ApiImplicitParam(name = "updateby", value = "updateby", dataType = "string", paramType = "query"),
        @ApiImplicitParam(name = "pageNo", value = "pageNo", dataType = "int", paramType = "query"),
        @ApiImplicitParam(name = "pageSize", value = "pageSize", dataType = "int", paramType = "query")
    })
    public Result<Page<TGroupsDto>> page(
                        @RequestParam(required = false) Integer id,
                        @RequestParam(required = false) String customerId,
                        @RequestParam(required = false) String name,
                        @RequestParam(required = false) String comments,
                        @RequestParam(required = false) String timeChannels,
                        @RequestParam(required = false) String deleted,
                        @RequestParam(required = false) String ctime,
                        @RequestParam(required = false) String utime,
                        @RequestParam(required = false) String createby,
                        @RequestParam(required = false) String updateby,
                    @RequestParam(required = true) Integer pageNo,
                    @RequestParam(required = true) Integer pageSize) {

        Map<String, String> params = new HashMap<>();

                params.put("customerId",customerId);
                params.put("name",name);
                params.put("comments",comments);
                params.put("timeChannels",timeChannels);
                params.put("deleted",deleted);
                params.put("ctime",ctime);
                params.put("utime",utime);
                params.put("createby",createby);
                params.put("updateby",updateby);
        return Result.ok(service.page(pageNo, pageSize, params));
    }

    @ApiOperation(value = "设置分组关联的用户", notes = "设置分组关联的用户")
    @RequestMapping(value = "/updateUsersByGroupId", method = RequestMethod.PUT)
    public Result<Boolean> updateUsersByGroupId(@RequestBody GroupUsersDto dto){
        return service.updateUsersByGroupId(dto);

    }

    @ApiOperation(value = "设置分组关联的物品", notes = "设置分组关联的物品")
    @RequestMapping(value = "/updateObjectsByGroupId", method = RequestMethod.PUT)
    public Result<Boolean> updateObjectsByGroupId(@RequestBody GroupObjectsDto dto){
        return service.updateObjectsByGroupId(dto);
    }

    @ApiOperation(value = "获取分组关联的用户", notes = "获取分组关联的用户")
    @RequestMapping(value = "/getUsersByGroupId/{groupId}", method = RequestMethod.GET)
    public Result<GroupUsersDto> getUsersByGroupId(@PathVariable Integer groupId){
        return service.getUsersByGroupId(groupId);
    }
    @ApiOperation(value = "获取分组关联的物品", notes = "获取分组关联的物品")
    @RequestMapping(value = "/getObjectsByGroupId/{groupId}", method = RequestMethod.GET)
    public Result<GroupObjectsDto> getObjectsByGroupId(@PathVariable Integer groupId){
        return service.getObjectsByGroupId(groupId);
    }
}